package com.peng.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.peng.pojo.OpenCourse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.peng.mapper.OpenCourseMapper;

import java.util.List;

@Service
public class OpenCourseServiceImp implements OpenCourseService {
    @Autowired
    private OpenCourseMapper openCourseMapper;

    @Override
    public boolean updateOpenCourseStatus(int status, Integer... teacherId) {
        openCourseMapper.updateOpenCourseStatus(status, teacherId);
        return true;
    }

    @Override
    public boolean deleteOpenCourse(Integer... teacherId) {
        List<OpenCourse> course = openCourseMapper.selectOpenCourseIdByTeacherId(teacherId);
        openCourseMapper.deleteTeacherThumbs(teacherId);//删除关于教师点赞
        openCourseMapper.deleteOpenTeacherComment(teacherId);//删除关于教师留言信息
        Integer[] ins = null;
        if (course != null) {
            ins = new Integer[course.size()];
            for (int i = 0; i < course.size(); i++) {
                ins[i] = course.get(i).getId();
            }
        }
        if (ins != null) {
            openCourseMapper.deleteOpenSignByCourseId(ins);//删除参加公开课人员信息
            openCourseMapper.delete0penResourcesByCourseId(ins); //删除教师相关公开课资源
        }
        openCourseMapper.deleteOpenCourseByTeacherId(teacherId);//删除教师相关公开课
        return true;
    }

    @Override
    public List<OpenCourse> selectOpenCourseIdByTeacherId(Integer... teacherId) {
        return openCourseMapper.selectOpenCourseIdByTeacherId(teacherId);
    }

    @Override
    public PageInfo<OpenCourse> selectAllOpenCourse(String name, Integer teacherId, Integer typeId, int page, int size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(openCourseMapper.selectAllOpenCourse(name, teacherId, typeId));
    }

    @Override
    public boolean updateOpenCourseStatusById(int status, Integer... id) {
        if (id != null) {
            openCourseMapper.updateOpenCourseStatusById(status, id);
        }
        return true;
    }

    @Override
    public List<OpenCourse> selectOpenCourseClassTimeDesc() {
        return openCourseMapper.selectOpenCourseClassTimeDesc();
    }
}
